<?php
/*+--------------------------------------------------------------------
 * 作	  者: xinglou <xinglou1@foxmail.com>
 +----------------------------------------------------------------------
 * 创建日期：2016-10-06
 +----------------------------------------------------------------------
 * 文件描述：得分管理
 +----------------------------------------------------------------------
 * 升级记录：
 +----------------------------------------------------------------------
*/
header("Content-Type:text/html;Charset=utf-8");
class ScoreAction extends CommonAction {
	/**
	 * 得分管理
	 */
	public function index() {
		$model = new GroupsUserModel();
   		//条件检索
		if(!empty($_REQUEST['search_keyword'])) {
			$where['name|real_name|professional|phone|email'] = array('like', '%'.$_REQUEST['search_keyword'].'%');
		}
		//通过省市县检索
		if(!empty($_REQUEST['school'])) {
			$where['school'] = $_REQUEST['school'];
		} elseif(!empty($_REQUEST['area_code'])) {
			$where['area_code'] = $_REQUEST['area_code'];
		} elseif(!empty($_REQUEST['city_code'])) {
			$where['area_code'] = array('like', $_REQUEST['city_code'].'%');
		} elseif(!empty($_REQUEST['province_code'])) {
			$where['area_code'] = array('like', $_REQUEST['province_code'].'%');
		}
		if(!empty($_REQUEST['type'])) {
			$where['type'] = $_REQUEST['type'];
		}
		$where['is_useful'] = '1';
//		dump($where);
//		dump($_REQUEST);
//		exit;
		
		//分页处理
		import('ORG.Util.Page');// 导入分页类
		$count = $model->where($where)->count();  // 查询满足要求的总记录数
		
		$p = $this->_page($model, 'id');  //分页所需信息
		$page = new Page($count, $p['listRows']);  // 实例化分页类 传入总记录数和每页显示的记录数
		
 		$data = $model->field("id,
 							   name,
 							   real_name,
							   type types,
 							   get_field_dict_name('groups_user', 'type', type) type,
 							   get_field_dict_name('user', 'gender', gender) gender,
 							   area_code,
 							   get_area_name(area_code) area_name,
 							   get_school_name(school) school,
 							   get_field_dict_name('groups_user','school_type',school_type) school_type,
 							   get_item_num(id) item_num,
 							   get_item_num_total(id) value_num,
 							   get_formate_time(begin_time) begin_time,
 							   get_formate_time(end_time) end_time,
 							   time_format(timediff(end_time, begin_time),'%H:%i:%s') used_time,
 							   feedback,
 							   get_score(id) score,
 							   get_total_score(id) total_score,
 							   get_user_name(operator) operator,
							   get_formate_date(update_date) update_date")  
 					  ->where($where)
 					  ->order("`".$p['order']."` ".$p['sort'])
					  ->limit($page->firstRow.', '.$page->listRows)		 
				 	  ->select();
		
//		dump($data);
//		dump($model->_sql());
//exit;
		$this->assign('data', $data);
		$show = $page->show();	//分页显示输出
		$this->assign('show', $show);	//绑定分页显示字符串
		$this->assign('type_list', set_field_dict_list('groups_user', 'type'));
		
		$this->assign('province_list', get_province_list());	//省份列表
		//查询该省份下的市州列表
		if(!empty($_REQUEST['province_code'])) {
			$model = new CityModel();
			$city_list = $model->field("code, name")
							   ->where("province_code = '".$_REQUEST['province_code']."'")
							   ->select();
			$this->assign('city_list', $city_list);
		}
		//查询该市州下的县区列表
		if(!empty($_REQUEST['city_code'])) {
			$model = new AreaModel();
			$area_list = $model->field("code, name")
							   ->where("city_code = '".$_REQUEST['city_code']."'")
							   ->select();
			$this->assign('area_list', $area_list);
		} 
		//查询该县域下的学校列表
		if(!empty($_REQUEST['area_code'])) {
			$model = new SchoolModel();
			$school_list = $model->field("id, name")
								 ->where("area_code = '".$_REQUEST['area_code']."'")
								 ->select();
			$this->assign('school_list', $school_list);
		} else {
			$model = new SchoolModel();
			$school_list = $model->field("id, name")
								 ->select();
			$this->assign('school_list', $school_list);
		}
		$this->display();
	}
	
	/**
	 * 批量下载用户问卷详细数据（选项+得分）
	 */
	public function download_bat() {
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		$this->download_detail($user_type, $area_code, $school, 'all');
	}
	
	/**
	 * 批量下载用户问卷详细数据（选项序号）
	 */
	public function download_bat_seq() {
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		$this->download_detail($user_type, $area_code, $school, 'seq');
	}
	
	/**
	 * 批量下载用户问卷详细数据(仅得分)
	 */
	public function download_bat_score() {
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		$this->download_detail($user_type, $area_code, $school, 'score');
	}
	
	/**
	 * 批量下载用户问卷详细数据(仅得分,按题项序号)
	 */
	public function download_bat_score_seq() {
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		$this->download_detail($user_type, $area_code, $school, 'score_seq');
	}
	
	/**
	 * 下载问卷详细数据
	 * @param string $user_type 用户类型
	 * @param string $area_code 县域代码
	 * @param string $school 学校代码
	 * @param string $type 类型，all:选项+得分，score:仅得分
	 * @param string $groups_userid 用户编号
	 */
	public function download_detail($user_type, $area_code, $school, $type = 'all', $groups_userid = null) {
		set_time_limit(0);
		ini_set("memory_limit", "-1");
		$formid = get_system_option("问卷".$user_type);
		//查询问卷信息
		$form = new FormModel();
		$form_data = $form->field("title,
								   item_info")
						  ->where("id = '".$formid."'")
						  ->find();
		$question_array = explode(",", $form_data['item_info']);
//		dump($item_array);
		//查询用户
		$groups_user = new GroupsUserModel();
		if(empty($user_type)) {
			$this->error("没有找到用户类型");
		}
		if(empty($area_code)) {
			$this->error("县区 范围不能为空");
		}
		switch ($user_type) {
			case 'a':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'b':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'c':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
				break;
			case 'd':
				$where['area_code'] = $area_code;
				break;
			default:
				$this->error("错误的用户类型");
		}
		$where['type'] = $user_type;
		$where['is_useful'] = '1';
		if(!empty($groups_userid)) {
			$where['id'] = $groups_userid;
		}
		$user_data = $groups_user->field("id,
										  type,
										  get_field_dict_name('groups_user','type',type) type_name,
										  name,
										  real_name,
 							   			  get_field_dict_name('user', 'gender', gender) gender,
										  birth,
										  area_code,
										  get_area_name(area_code) area_name,
										  school,
										  get_school_name(school) school_name,
										  get_field_dict_name('groups_user','school_type',school_type) school_type,
										  get_field_dict_name('groups_user','phase',phase) phase,
										  get_field_dict_name('groups_user','grade',grade) grade,
										  get_class_name(class) class_name,
										  get_field_dict_name('groups_user','subject',subject) subject,
										  get_field_dict_name('groups_user','teach_year',teach_year) teach_year,
										  page,
			 							  get_item_num(id) item_num,
			 							  get_item_num_total(id) value_num,
			 							  get_formate_time(begin_time) begin_time,
			 							  get_formate_time(end_time) end_time,
			 							  time_format(timediff(end_time, begin_time),'%H:%i:%s') used_time,
			 							  feedback,
 										  get_score(id) score")
								 ->where($where)
								 ->order('area_code, school')
								 ->select();
//		dump($where);
//		dump($user_data);
//		exit;
		//添加表头
		$head = array();   //表头数组
		$detail = array();     //问卷数据
//		$detail[0]['name'] = "用户名";
		$detail[0]['real_name'] = "真实姓名";
		$detail[0]['gender'] = "性别";
		switch($user_type) {
			case 'a':
				$detail[0]['birth'] = "出生年月";
				$detail[0]['area_code'] = "所属县域";
				$detail[0]['school'] = "所在学校";
				$detail[0]['school_type'] = "学校类型";
				$detail[0]['phase'] = "学段";
				$detail[0]['grade'] = "年级";
				$detail[0]['class_name'] = "班级";
				$head_pre = 'S';
				break;
			case 'b':
				$detail[0]['birth'] = "出生年月";
				$detail[0]['area_code'] = "所属县域";
				$detail[0]['school'] = "所在学校";
				$detail[0]['school_type'] = "学校类型";
				$detail[0]['teach_year'] = "教龄";
				$detail[0]['phase'] = "学段";
				$detail[0]['subject'] = "教授学科";
				$detail[0]['grade'] = "教授年级";
				$detail[0]['class_name'] = "教授班级";
				$head_pre = 'T';
				break;
			case 'c':
				$detail[0]['area_code'] = "所属县域";
				$detail[0]['school'] = "所在学校";
				$detail[0]['school_type'] = "学校类型";
				$head_pre = 'E';
				break;
			case 'd':
				$detail[0]['area_code'] = "所属县域";
				$head_pre = 'M';
				break;
		}
		$detail[0]['item_num'] = "提交题项数量";
		$detail[0]['value_num'] = "提交信息数量";
		if($type != 'seq') {
			$detail[0]['begin_time'] = "开始时间";
			$detail[0]['end_time'] = "结束时间";
		}
		$detail[0]['used_time'] = "所用时间";
		$detail[0]['feedback'] = "反馈";
		if($type != 'seq') {
			$detail[0]['score'] = "总得分";
		}
		foreach ($question_array as $q_key => $q_value) {
			$temp_question = explode(":", $q_value);
//			dump($temp_item);
			$questionid = $temp_question[1];
			$question_data = $form->table("question_test_select")
								   ->field("code,
								   			question title")
								   ->where("id = '".$questionid."'")
								   ->union("select code, question title from blank where id = '".$questionid."'")
								   ->union("select code, title from affix where id = '".$questionid."'")
								   ->find();
			$question_data['title'] = $this->cutstr_html($question_data['title'], 100);
//			dump($question_data);
			if(!empty($questionid)) {
				if($type == 'all') {
					$head[$questionid] = '['.$question_data['code'].']'.str_replace('‍‍', '', $question_data['title']);
					$detail[0][$questionid] = str_replace('​', '', str_replace('‍', '', $head[$questionid]));
					$detail[0][$questionid.'_score'] = '得分';
				} elseif($type == 'seq') {
					$head[$questionid] = $question_data['code'];
					$detail[0][$questionid] = str_replace('​', '', str_replace('‍', '', $head[$questionid]));
				} elseif($type == 'score_seq') {
					$head[$questionid] = $head_pre.'_'.($q_key + 1);
					$detail[0][$questionid.'_score'] = str_replace('​', '', str_replace('‍', '', $head[$questionid]));
				} else {
					$head[$questionid] = $question_data['code'];
					$detail[0][$questionid.'_score'] = str_replace('​', '', str_replace('‍', '', $head[$questionid]));
				}
			}
		}
//		dump($head);
//		exit;
//		dump(chr(33));
//		dump($detail);
//		exit;
		//组装问卷提交数据
		$item_value = new ItemValueModel();
		$score_model = new ScoreModel();
		foreach ($user_data as $u_key => $u_value) {
//			$detail[$u_key + 1]['name'] = $u_value['name'];
			$detail[$u_key + 1]['real_name'] = $u_value['real_name'];
			$detail[$u_key + 1]['gender'] = $u_value['gender'];
			switch($user_type) {
				case 'a':
					$detail[$u_key + 1]['birth'] = $u_value['birth'];
					$detail[$u_key + 1]['area_code'] = $u_value['area_name'];
					$detail[$u_key + 1]['school'] = $u_value['school_name'];
					$detail[$u_key + 1]['school_type'] = $u_value['school_type'];
					$detail[$u_key + 1]['phase'] = $u_value['phase'];
					$detail[$u_key + 1]['grade'] = $u_value['grade'];
					$detail[$u_key + 1]['class_name'] = $u_value['class_name'];
					break;
				case 'b':
					$detail[$u_key + 1]['birth'] = $u_value['birth'];
					$detail[$u_key + 1]['area_code'] = $u_value['area_name']; //$u_value['area_code'].' '.
					$detail[$u_key + 1]['school'] = $u_value['school_name'];
					$detail[$u_key + 1]['school_type'] = $u_value['school_type'];
					$detail[$u_key + 1]['teach_year'] = $u_value['teach_year'];
					$detail[$u_key + 1]['phase'] = $u_value['phase'];
					$detail[$u_key + 1]['subject'] = $u_value['subject'];
					$detail[$u_key + 1]['grade'] = $u_value['grade'];
					$detail[$u_key + 1]['class_name'] = $u_value['class_name'];
					break;
				case 'c':
					$detail[$u_key + 1]['area_code'] = $u_value['area_name'];
					$detail[$u_key + 1]['school'] = $u_value['school_name'];
					$detail[$u_key + 1]['school_type'] = $u_value['school_type'];
					break;
				case 'd':
					$detail[$u_key + 1]['area_code'] = $u_value['area_name'];
					break;
			}
			$detail[$u_key + 1]['item_num'] = $u_value['item_num'];
			$detail[$u_key + 1]['value_num'] = $u_value['value_num'];
			if($type != 'seq') {
				$detail[$u_key + 1]['begin_time'] = $u_value['begin_time'];
				$detail[$u_key + 1]['end_time'] = $u_value['end_time'];
			}
			$detail[$u_key + 1]['used_time'] = $u_value['used_time'];
			$detail[$u_key + 1]['feedback'] = $u_value['feedback'];
			if($type != 'seq') {
				$detail[$u_key + 1]['score'] = $u_value['score'];
			}
//			dump($detail);
//			exit;
			foreach ($head as $h_key => $h_value) {
				if($type == 'all') {
					$value = $item_value->field('get_item_value(questionid, operator) value')->where("formid = '".$formid."' and questionid = '".$h_key."' and operator = '".$u_value['id']."'")->find();
					if(!empty($value)) {
						$detail[$u_key + 1][$h_key] = str_replace(' ', '', $value['value']);
					} else {
	//					$detail[$u_key + 1][$h_key] = '***'.$item_value->_sql();
						$detail[$u_key + 1][$h_key] = '';
					}
					$score = $score_model->field("score, total_score, get_question_score('".$h_key."') question_score")->where("questionid = '".$h_key."' and groups_userid = '".$u_value['id']."'")->find();
					if(!empty($score)) {
						$detail[$u_key + 1][$h_key.'_score'] = $score['score']."/".$score['question_score'];
					} else {
						$question_score = $score_model->field("get_question_score('".$h_key."') question_score")->find();
						$detail[$u_key + 1][$h_key.'_score'] = "0/".$question_score['question_score'];
					}
				} elseif($type == 'seq') {
					//$value = $score_model->field('item_seq seq')->where("questionid = '".$h_key."' and groups_userid = '".$u_value['id']."'")->find();
					$value = $item_value->field('get_item_value_seq(questionid, operator) seq')->where("formid = '".$formid."' and questionid = '".$h_key."' and operator = '".$u_value['id']."'")->find();
					if(!empty($value)) {
						$detail[$u_key + 1][$h_key] = str_replace(' ', '', $value['seq']);
					} else {
						$detail[$u_key + 1][$h_key] = '';
					}
				} else {
					$score = $score_model->field("score, total_score, get_question_score('".$h_key."') question_score")->where("questionid = '".$h_key."' and groups_userid = '".$u_value['id']."'")->find();
					if(!empty($score)) {
						$detail[$u_key + 1][$h_key.'_score'] = $score['score'];
					} else {
						$detail[$u_key + 1][$h_key.'_score'] = "0";
					}
				}
			}
		}
		
//		dump($detail);
//		exit;
		//组建文件名称
		if(!empty($area_code)) {
			$file_name_area = str_replace(' ', '_', $user_data[0]['area_name']);
		} else {
			$file_name_area = '';
		}
		if(!empty($school)) {
			$file_name_school = str_replace(' ', '', $user_data[0]['school_name']);
		} else {
			$file_name_school = '';
		}
		$file_name_type = $user_data[0]['type_name'];
		if($type == 'all') {
			$file_name = $file_name_area.'_'.$file_name_school.'_'.$user_data[0]['type_name'].'_问卷数据和得分详细情况';      	//excel文件名
		} elseif($type == 'seq') {
			$file_name = $file_name_area.'_'.$file_name_school.'_'.$user_data[0]['type_name'].'_问卷数据选项序号详细情况';      	//excel文件名
		} else {
			$file_name = $file_name_area.'_'.$file_name_school.'_'.$user_data[0]['type_name'].'_问卷得分详细情况';      	//excel文件名
		}
		
		//将用户信息组装成表格导出到excel中
		$str = '';	//初始化
		//输出表头
		$str .= iconv("UTF-8","GBK",'<table border="1">');
//		$str .= iconv("UTF-8","GBK",'<table border="1"><tr><td colspan="'.count($detail[0]).'" align="center" style="font-size:16px; height:32px; font-weight:bold">'.$file_name.'</td></tr>');
		//循环输出行		
		foreach($detail as $v1) {
			$str .= iconv("UTF-8","GBK",'<tr>');
			//循环输出列
//			dump($v1);
//			exit;
			foreach($v1 as $v2) {
				$temp_str = '<td style="vnd.ms-excel.numberformat:@">'.$v2.'</td>';
				$str .= iconv("UTF-8","GBK",$temp_str);
//				dump($v2);
			}
			$str .= iconv("UTF-8","GBK",'</tr>');
//			dump($str);
//			exit;
		}
		$str .=	iconv("UTF-8","GBK",'</table>');
		//替换
//		$str = str_replace(' ', '', $str);
//		$str = str_replace('$$$', "<br/>", $str);
//		dump($str);
//		echo $str; 
//		exit;
		
		//转换为GBK编码
		//$str = iconv("UTF-8","GBK",$str);
		 
		//输出
		ob_end_clean();//清除缓冲区,避免乱码
		header('Content-Length: '.strlen($str));
		header("content-Type:text/html; charset=gbk");
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:filename={$file_name}.xls");
		echo $str;
	}
	
	/**
	 * 问卷详细数据（SPSS分析）
	 */
	public function download_spss() {
		set_time_limit(0);
		ini_set("memory_limit", "-1");
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		
		$formid = get_system_option("问卷".$user_type);
		//查询问卷信息
		$form = new FormModel();
		$form_data = $form->field("title,
								   item_info")
						  ->where("id = '".$formid."'")
						  ->find();
		$question_array = explode(",", $form_data['item_info']);
//		dump($item_array);
		//查询用户
		$groups_user = new GroupsUserModel();
		if(empty($user_type)) {
			$this->error("没有找到用户类型");
		}
		if($user_type == 'c' || $user_type == 'd')
		if(empty($area_code)) {
			$this->error("用户类型 必须为 学生 或 教师");
		}
		switch ($user_type) {
			case 'a':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'b':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'c':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
				break;
			case 'd':
				$where['area_code'] = $area_code;
				break;
			default:
				$this->error("错误的用户类型");
		}
		$where['type'] = $user_type;
		$where['is_useful'] = '1';
		$user_data = $groups_user->field("id,
										  type,
										  get_field_dict_name('groups_user','type',type) type_name,
										  real_name,
 							   			  get_field_dict_name('user', 'gender', gender) gender,
										  birth,
										  year(now()) - left(birth,4) age,
										  area_code,
										  get_area_name(area_code) area_name,
										  school,
										  get_school_name(school) school_name,
										  get_field_dict_name('groups_user','school_type',school_type) school_type,
										  get_field_dict_name('groups_user','phase',phase) phase,
										  get_field_dict_name('groups_user','grade',grade) grade,
										  get_class_name(class) class_name,
										  get_field_dict_name('groups_user','subject',subject) subject,
										  get_field_dict_name('groups_user','teach_year',teach_year) teach_year")
								 ->where($where)
								 ->order('area_code, school')
//								 ->limit(10)
								 ->select();
//		dump($where);
//		dump($user_data);
//		dump($groups_user->_sql());
//		exit;
		//添加表头
		$head = array();   //表头数组
		$detail = array();     //问卷数据
//		$detail[0]['name'] = "用户名";
		$detail[0]['province'] = "省";
		$detail[0]['city'] = "市";
		$detail[0]['area'] = "县";
		$detail[0]['real_name'] = "姓名";
		$detail[0]['gender'] = "性别";
		switch($user_type) {
			case 'a':
//				$detail[0]['birth'] = "出生年月";
//				$detail[0]['area_code'] = "所属县域";
				$detail[0]['phase'] = "学段";
				$detail[0]['school'] = "学校";
				$detail[0]['school_type'] = "学校类型";
				$detail[0]['grade'] = "年级";
				$detail[0]['class_name'] = "班级";
				$detail[0]['age'] = "年龄";
				$head_pre = 'S';
				break;
			case 'b':
				$detail[0]['phase'] = "学段";
				$detail[0]['school'] = "学校";
				$detail[0]['school_type'] = "学校类型";
				$detail[0]['age'] = "年龄";
				$detail[0]['teach_year'] = "教龄";
				$detail[0]['subject'] = "学科";
//				$detail[0]['birth'] = "出生年月";
//				$detail[0]['area_code'] = "所属县域";
//				$detail[0]['school'] = "所在学校";
//				$detail[0]['teach_year'] = "教龄";
//				$detail[0]['phase'] = "学段";
//				$detail[0]['subject'] = "教授学科";
//				$detail[0]['grade'] = "教授年级";
//				$detail[0]['class_name'] = "教授班级";
				$head_pre = 'T';
				break;
			case 'c':
				$detail[0]['area_code'] = "所属县域";
				$detail[0]['school'] = "所在学校";
				$detail[0]['school_type'] = "学校类型";
				$head_pre = 'E';
				break;
			case 'd':
				$detail[0]['area_code'] = "所属县域";
				$head_pre = 'M';
				break;
		}
//		dump($detail);
//		exit;
//		dump($question_array);
//		exit;
		foreach ($question_array as $q_key => $q_value) {
			$temp_question = explode(":", $q_value);
			$questionid = $temp_question[1];
//			dump($temp_item);
			$question_data = $form->table("question_test_select")
								   ->field("id,
								   			code,
								   			type")
								   ->where("id = '".$questionid."'")
								   ->union("select id, code, 'blank' type from blank where id = '".$questionid."'")
								   ->union("select id, code, 'affix' type from affix where id = '".$questionid."'")
								   ->find();
			if(empty($question_data)) {
				continue;
			}
			if($question_data['type'] == 'b') {  // 如果是多选题，查询选项
				$item_array = $form->table("question_test_select_item")
								   ->field("id,
								   			seq")
								   ->where("selectid = '".$questionid."'")
								   ->order('seq')
								   ->select();
				foreach($item_array as $key => $value) {
					$head_name = $head_pre.'_'.($q_key + 1).'_'.$this->char_num_exchange($value['seq']);
					$head[$head_name] = $questionid.'*'.$value['seq'];
					$detail[0][$head_name] = $head_name;
				}
			} else {
				$head_name = $head_pre.'_'.($q_key + 1);
				$head[$head_name] = $questionid;
				$detail[0][$head_name] = $head_name;
			}
		}
//		dump($head);
//		dump($detail);
//		exit;
		//组装问卷提交数据
		$item_value = new ItemValueModel();
		$score_model = new ScoreModel();
		foreach ($user_data as $u_key => $u_value) {
			$area_array = explode(' ', $u_value['area_name']);
			$detail[$u_key + 1]['province'] = $area_array[0];
			$detail[$u_key + 1]['city'] = $area_array[1];
			$detail[$u_key + 1]['area'] = $area_array[2];
			$detail[$u_key + 1]['real_name'] = $u_value['real_name'];
			$detail[$u_key + 1]['gender'] = $u_value['gender'];
			switch($user_type) {
				case 'a':
					$detail[$u_key + 1]['phase'] = $u_value['phase'];
					$detail[$u_key + 1]['school'] = $u_value['school_name'];
					$detail[$u_key + 1]['school_type'] = $u_value['school_type'];
					$detail[$u_key + 1]['grade'] = $u_value['grade'];
					$detail[$u_key + 1]['class_name'] = $u_value['class_name'];
					$detail[$u_key + 1]['age'] = $u_value['age'];
					break;
				case 'b':
					$detail[$u_key + 1]['phase'] = $u_value['phase'];
					$detail[$u_key + 1]['school'] = $u_value['school_name'];
					$detail[$u_key + 1]['school_type'] = $u_value['school_type'];
					$detail[$u_key + 1]['age'] = $u_value['age'];
					$detail[$u_key + 1]['teach_year'] = $u_value['teach_year'];
					$detail[$u_key + 1]['subject'] = $u_value['subject'];
//					$detail[$u_key + 1]['birth'] = $u_value['birth'];
//					$detail[$u_key + 1]['area_code'] = $u_value['area_name']; //$u_value['area_code'].' '.
//					$detail[$u_key + 1]['grade'] = $u_value['grade'];
//					$detail[$u_key + 1]['class_name'] = $u_value['class_name'];
					break;
				case 'c':
					$detail[$u_key + 1]['area_code'] = $u_value['area_name'];
					$detail[$u_key + 1]['school'] = $u_value['school_name'];
					$detail[$u_key + 1]['school_type'] = $u_value['school_type'];
					break;
				case 'd':
					$detail[$u_key + 1]['area_code'] = $u_value['area_name'];
					break;
			}
//			dump($detail);
//			exit;
			foreach ($head as $h_key => $h_value) {
				if(empty($h_value)) {
					continue;
				}
				$head_info = explode('*', $h_value);
				$score = $score_model->where("groups_userid = '".$u_value['id']."' and questionid = '".$head_info[0]."'")->getField('question_seq');
				if(empty($score)) {
					$detail[$u_key + 1][$h_key] = '-1';
				} elseif(count($head_info) < 2) {
					$detail[$u_key + 1][$h_key] = str_replace('<br/>', ',', str_replace(',', '', $score));
				} else {
					if(strpos($score, $head_info[1].',') !== false) {
						$detail[$u_key + 1][$h_key] = '1';
					} else {
						$detail[$u_key + 1][$h_key] = '0';
					}
				}
			}
		}
//		dump($detail);
//		exit;

		//组建文件名称
		if(!empty($area_code)) {
			$file_name_area = str_replace(' ', '_', $user_data[0]['area_name']);
		} else {
			$file_name_area = '';
		}
		if(!empty($school)) {
			$file_name_school = str_replace(' ', '', $user_data[0]['school_name']);
		} else {
			$file_name_school = '';
		}
		$file_name_type = $user_data[0]['type_name'];
		$file_name = $file_name_area.'_'.$file_name_school.'_'.$user_data[0]['type_name'].'_问卷详细情况_SPSS分析';      	//excel文件名
		
		//将用户信息组装成表格导出到excel中
		$str = '';	//初始化
		//输出表头
		$str .= iconv("UTF-8","GBK",'<table border="1">');
//		$str .= iconv("UTF-8","GBK",'<table border="1"><tr><td colspan="'.count($detail[0]).'" align="center" style="font-size:16px; height:32px; font-weight:bold">'.$file_name.'</td></tr>');
		//循环输出行		
		foreach($detail as $v1) {
			$str .= iconv("UTF-8","GBK",'<tr>');
			//循环输出列
//			dump($v1);
//			exit;
			foreach($v1 as $v2) {
				$temp_str = '<td style="vnd.ms-excel.numberformat:@">'.$v2.'</td>';
				$str .= iconv("UTF-8","GBK",$temp_str);
//				dump($v2);
			}
			$str .= iconv("UTF-8","GBK",'</tr>');
//			dump($str);
//			exit;
		}
		$str .=	iconv("UTF-8","GBK",'</table>');
		//替换
//		$str = str_replace(' ', '', $str);
		header("content-Type:text/html; charset=gbk");
//		$str = str_replace('$$$', "<br/>", $str);
//		dump($str);
//		echo $str; 
//		exit;
		
		//转换为GBK编码
//		$str = iconv("UTF-8","GBK",$str);
		 
		//输出
		ob_end_clean();//清除缓冲区,避免乱码
		header('Content-Length: '.strlen($str));
		header("content-Type:text/html; charset=gbk");
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:attachment;filename={$file_name}.xls");
		echo $str;
	}
	
	/**
	 * 问卷详细数据（SPSS分析）
	 */
	public function download_spss_select() {
		set_time_limit(0);
		ini_set("memory_limit", "-1");
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		
		$formid = get_system_option("问卷".$user_type);
		//查询问卷信息
		$form = new FormModel();
		$form_data = $form->field("title,
								   item_info")
						  ->where("id = '".$formid."'")
						  ->find();
		$question_array = explode(",", $form_data['item_info']);
//		dump($item_array);
		//查询用户
		$groups_user = new GroupsUserModel();
		if(empty($user_type)) {
			$this->error("没有找到用户类型");
		}
		if($user_type == 'c' || $user_type == 'd')
		if(empty($area_code)) {
			$this->error("用户类型 必须为 学生 或 教师");
		}
		switch ($user_type) {
			case 'a':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'b':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'c':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
				break;
			case 'd':
				$where['area_code'] = $area_code;
				break;
			default:
				$this->error("错误的用户类型");
		}
		$where['type'] = $user_type;
		$where['is_useful'] = '1';
		$user_data = $groups_user->field("id,
										  type,
										  get_field_dict_name('groups_user','type',type) type_name,
										  real_name,
 							   			  get_field_dict_name('user', 'gender', gender) gender,
										  birth,
										  year(now()) - left(birth,4) age,
										  area_code,
										  get_area_name(area_code) area_name,
										  school,
										  get_school_name(school) school_name,
										  get_field_dict_name('groups_user','school_type',school_type) school_type,
										  get_field_dict_name('groups_user','phase',phase) phase,
										  get_field_dict_name('groups_user','grade',grade) grade,
										  get_class_name(class) class_name,
										  get_field_dict_name('groups_user','subject',subject) subject,
										  get_field_dict_name('groups_user','teach_year',teach_year) teach_year")
								 ->where($where)
								 ->order('area_code, school')
//								 ->limit(10)
								 ->select();
//		dump($where);
//		dump($user_data);
//		dump($groups_user->_sql());
//		exit;
		//添加表头
		$head = array();   //表头数组
		$detail = array();     //问卷数据
//		$detail[0]['name'] = "用户名";
		$detail[0]['province'] = "省";
		$detail[0]['city'] = "市";
		$detail[0]['area'] = "县";
		$detail[0]['real_name'] = "姓名";
		$detail[0]['gender'] = "性别";
		switch($user_type) {
			case 'a':
//				$detail[0]['birth'] = "出生年月";
//				$detail[0]['area_code'] = "所属县域";
				$detail[0]['phase'] = "学段";
				$detail[0]['school'] = "学校";
				$detail[0]['school_type'] = "学校类型";
				$detail[0]['grade'] = "年级";
				$detail[0]['class_name'] = "班级";
				$detail[0]['age'] = "年龄";
				$head_pre = 'S';
				break;
			case 'b':
				$detail[0]['phase'] = "学段";
				$detail[0]['school'] = "学校";
				$detail[0]['school_type'] = "学校类型";
				$detail[0]['age'] = "年龄";
				$detail[0]['teach_year'] = "教龄";
				$detail[0]['subject'] = "学科";
//				$detail[0]['birth'] = "出生年月";
//				$detail[0]['area_code'] = "所属县域";
//				$detail[0]['school'] = "所在学校";
//				$detail[0]['teach_year'] = "教龄";
//				$detail[0]['phase'] = "学段";
//				$detail[0]['subject'] = "教授学科";
//				$detail[0]['grade'] = "教授年级";
//				$detail[0]['class_name'] = "教授班级";
				$head_pre = 'T';
				break;
			case 'c':
				$detail[0]['area_code'] = "所属县域";
				$detail[0]['school'] = "所在学校";
				$detail[0]['school_type'] = "学校类型";
				$head_pre = 'E';
				break;
			case 'd':
				$detail[0]['area_code'] = "所属县域";
				$head_pre = 'M';
				break;
		}
//		dump($detail);
//		exit;
//		dump($question_array);
//		exit;
		foreach ($question_array as $q_key => $q_value) {
			$temp_question = explode(":", $q_value);
			if($temp_question[0] == 'select') {
				$questionid = $temp_question[1];
	//			dump($temp_item);
				$question_data = $form->table("question_test_select")
									   ->field("id,
									   			code,
									   			type")
									   ->where("id = '".$questionid."'")
									   ->find();
				if(empty($question_data)) {
					continue;
				}
				if($question_data['type'] == 'b') {  // 如果是多选题，查询选项
					$item_array = $form->table("question_test_select_item")
									   ->field("id,
									   			seq")
									   ->where("selectid = '".$questionid."'")
									   ->order('seq')
									   ->select();
					foreach($item_array as $key => $value) {
						$head_name = $head_pre.'_'.($q_key + 1).'_'.$this->char_num_exchange($value['seq']);
						$head[$head_name] = $questionid.'*'.$value['seq'];
						$detail[0][$head_name] = $head_name;
					}
				} else {
					$head_name = $head_pre.'_'.($q_key + 1);
					$head[$head_name] = $questionid;
					$detail[0][$head_name] = $head_name;
				}
			}
		}
//		dump($head);
//		dump($detail);
//		exit;
		//组装问卷提交数据
		$item_value = new ItemValueModel();
		$score_model = new ScoreModel();
		foreach ($user_data as $u_key => $u_value) {
			$area_array = explode(' ', $u_value['area_name']);
			$detail[$u_key + 1]['province'] = $area_array[0];
			$detail[$u_key + 1]['city'] = $area_array[1];
			$detail[$u_key + 1]['area'] = $area_array[2];
			$detail[$u_key + 1]['real_name'] = $u_value['real_name'];
			$detail[$u_key + 1]['gender'] = $u_value['gender'];
			switch($user_type) {
				case 'a':
					$detail[$u_key + 1]['phase'] = $u_value['phase'];
					$detail[$u_key + 1]['school'] = $u_value['school_name'];
					$detail[$u_key + 1]['school_type'] = $u_value['school_type'];
					$detail[$u_key + 1]['grade'] = $u_value['grade'];
					$detail[$u_key + 1]['class_name'] = $u_value['class_name'];
					$detail[$u_key + 1]['age'] = $u_value['age'];
					break;
				case 'b':
					$detail[$u_key + 1]['phase'] = $u_value['phase'];
					$detail[$u_key + 1]['school'] = $u_value['school_name'];
					$detail[$u_key + 1]['school_type'] = $u_value['school_type'];
					$detail[$u_key + 1]['age'] = $u_value['age'];
					$detail[$u_key + 1]['teach_year'] = $u_value['teach_year'];
					$detail[$u_key + 1]['subject'] = $u_value['subject'];
//					$detail[$u_key + 1]['birth'] = $u_value['birth'];
//					$detail[$u_key + 1]['area_code'] = $u_value['area_name']; //$u_value['area_code'].' '.
//					$detail[$u_key + 1]['grade'] = $u_value['grade'];
//					$detail[$u_key + 1]['class_name'] = $u_value['class_name'];
					break;
				case 'c':
					$detail[$u_key + 1]['area_code'] = $u_value['area_name'];
					$detail[$u_key + 1]['school'] = $u_value['school_name'];
					$detail[$u_key + 1]['school_type'] = $u_value['school_type'];
					break;
				case 'd':
					$detail[$u_key + 1]['area_code'] = $u_value['area_name'];
					break;
			}
//			dump($detail);
//			exit;
			foreach ($head as $h_key => $h_value) {
				if(empty($h_value)) {
					continue;
				}
				$head_info = explode('*', $h_value);
				$score = $score_model->where("groups_userid = '".$u_value['id']."' and questionid = '".$head_info[0]."'")->getField('item_seq');
				if(empty($score)) {
					$detail[$u_key + 1][$h_key] = '-1';
				} elseif(count($head_info) < 2) {
					$detail[$u_key + 1][$h_key] = str_replace('<br/>', ',', str_replace(',', '', $score));
				} else {
					if(strpos($score, $head_info[1].',') !== false) {
						$detail[$u_key + 1][$h_key] = '1';
					} else {
						$detail[$u_key + 1][$h_key] = '0';
					}
				}
			}
		}
//		dump($detail);
//		exit;

		//组建文件名称
		if(!empty($area_code)) {
			$file_name_area = str_replace(' ', '_', $user_data[0]['area_name']);
		} else {
			$file_name_area = '';
		}
		if(!empty($school)) {
			$file_name_school = str_replace(' ', '', $user_data[0]['school_name']);
		} else {
			$file_name_school = '';
		}
		$file_name_type = $user_data[0]['type_name'];
		$file_name = $file_name_area.'_'.$file_name_school.'_'.$user_data[0]['type_name'].'_问卷详细情况_SPSS分析_select_only';      	//excel文件名
		
		//将用户信息组装成表格导出到excel中
		$str = '';	//初始化
		//输出表头
		$str .= iconv("UTF-8","GBK",'<table border="1">');
//		$str .= iconv("UTF-8","GBK",'<table border="1"><tr><td colspan="'.count($detail[0]).'" align="center" style="font-size:16px; height:32px; font-weight:bold">'.$file_name.'</td></tr>');
		//循环输出行		
		foreach($detail as $v1) {
			$str .= iconv("UTF-8","GBK",'<tr>');
			//循环输出列
//			dump($v1);
//			exit;
			foreach($v1 as $v2) {
				$temp_str = '<td style="vnd.ms-excel.numberformat:@">'.$v2.'</td>';
				$str .= iconv("UTF-8","GBK",$temp_str);
//				dump($v2);
			}
			$str .= iconv("UTF-8","GBK",'</tr>');
//			dump($str);
//			exit;
		}
		$str .=	iconv("UTF-8","GBK",'</table>');
		//替换
//		$str = str_replace(' ', '', $str);
		header("content-Type:text/html; charset=gbk");
//		$str = str_replace('$$$', "<br/>", $str);
//		dump($str);
//		echo $str; 
//		exit;
		
		//转换为GBK编码
//		$str = iconv("UTF-8","GBK",$str);
		 
		//输出
		ob_end_clean();//清除缓冲区,避免乱码
		header('Content-Length: '.strlen($str));
		header("content-Type:text/html; charset=gbk");
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:attachment;filename={$file_name}.xls");
		echo $str;
	}
	
	/**
	 * 问卷详细数据（指标项得分）
	 */
	public function download_third_quota() {
		set_time_limit(0);
		ini_set("memory_limit", "-1");
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		$model = new FormModel();
	
		$groups_user = new GroupsUserModel();
		if(empty($user_type)) {
			$this->error("没有找到用户类型");
		}
		if($user_type == 'c' || $user_type == 'd')
		if(empty($area_code)) {
			$this->error("用户类型 必须为 学生 或 教师");
		}
		switch ($user_type) {
			case 'a':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'b':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'c':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
				break;
			case 'd':
				$where['area_code'] = $area_code;
				break;
			default:
				$this->error("错误的用户类型");
		}
		$where['type'] = $user_type;
		$where['is_useful'] = '1';
		$user_data = $groups_user->field("id,
										  type,
										  get_field_dict_name('groups_user','type',type) type_name,
										  real_name,
 							   			  get_field_dict_name('user', 'gender', gender) gender,
										  birth,
										  year(now()) - left(birth,4) age,
										  area_code,
										  get_area_name(area_code) area_name,
										  school,
										  get_school_name(school) school_name,
										  get_field_dict_name('groups_user','school_type',school_type) school_type,
										  get_field_dict_name('groups_user','phase',phase) phase,
										  get_field_dict_name('groups_user','grade',grade) grade,
										  get_class_name(class) class_name,
										  get_field_dict_name('groups_user','subject',subject) subject,
										  get_field_dict_name('groups_user','teach_year',teach_year) teach_year")
								 ->where($where)
								 ->order('area_code, school')
//								 ->limit(10)
								 ->select();
		//查询所有指标项
		$quota = new QuotaModel();
		$quota_list = $quota->field("id, code")->order("code")->select();
//		dump($quota_list);
//		exit;
		$quota_table = array();
		$head = array();   //表头数组
//		$detail[0]['name'] = "用户名";
		$quota_table[0]['province'] = "省";
		$quota_table[0]['city'] = "市";
		$quota_table[0]['area'] = "县";
		$quota_table[0]['real_name'] = "姓名";
		$quota_table[0]['gender'] = "性别";
		switch($user_type) {
			case 'a':
//				$detail[0]['birth'] = "出生年月";
//				$detail[0]['area_code'] = "所属县域";
				$quota_table[0]['phase'] = "学段";
				$quota_table[0]['school'] = "学校";
				$quota_table[0]['school_type'] = "学校类型";
				$quota_table[0]['grade'] = "年级";
				$quota_table[0]['class_name'] = "班级";
				$quota_table[0]['age'] = "年龄";
				break;
			case 'b':
				$quota_table[0]['phase'] = "学段";
				$quota_table[0]['school'] = "学校";
				$quota_table[0]['school_type'] = "学校类型";
				$quota_table[0]['age'] = "年龄";
				$quota_table[0]['teach_year'] = "教龄";
				$quota_table[0]['subject'] = "学科";
//				$detail[0]['birth'] = "出生年月";
//				$detail[0]['area_code'] = "所属县域";
//				$detail[0]['school'] = "所在学校";
//				$detail[0]['teach_year'] = "教龄";
//				$detail[0]['phase'] = "学段";
//				$detail[0]['subject'] = "教授学科";
//				$detail[0]['grade'] = "教授年级";
//				$detail[0]['class_name'] = "教授班级";
				break;
			case 'c':
				$quota_table[0]['area_code'] = "所属县域";
				$quota_table[0]['school'] = "所在学校";
				$quota_table[0]['school_type'] = "学校类型";
				break;
			case 'd':
				$quota_table[0]['area_code'] = "所属县域";
				break;
		}
		foreach($quota_list as $key => $value) {
			$quota_table[0][$value['id']] = $value['code'];
		}
//		dump($quota_table);
//		exit;
		//组建指标表格
		$score = new ScoreModel();
		$where['_string'] = "a.quotaid = b.id and a.groups_userid = c.id";
		foreach($user_data as $u_key => $u_value) {
			$area_array = explode(' ', $u_value['area_name']);
			$quota_table[$u_key + 1]['province'] = $area_array[0];
			$quota_table[$u_key + 1]['city'] = $area_array[1];
			$quota_table[$u_key + 1]['area'] = $area_array[2];
			$quota_table[$u_key + 1]['real_name'] = $u_value['real_name'];
			$quota_table[$u_key + 1]['gender'] = $u_value['gender'];
			switch($user_type) {
				case 'a':
					$quota_table[$u_key + 1]['phase'] = $u_value['phase'];
					$quota_table[$u_key + 1]['school'] = $u_value['school_name'];
					$quota_table[$u_key + 1]['school_type'] = $u_value['school_type'];
					$quota_table[$u_key + 1]['grade'] = $u_value['grade'];
					$quota_table[$u_key + 1]['class_name'] = $u_value['class_name'];
					$quota_table[$u_key + 1]['age'] = $u_value['age'];
					break;
				case 'b':
					$quota_table[$u_key + 1]['phase'] = $u_value['phase'];
					$quota_table[$u_key + 1]['school'] = $u_value['school_name'];
					$quota_table[$u_key + 1]['school_type'] = $u_value['school_type'];
					$quota_table[$u_key + 1]['age'] = $u_value['age'];
					$quota_table[$u_key + 1]['teach_year'] = $u_value['teach_year'];
					$quota_table[$u_key + 1]['subject'] = $u_value['subject'];
//					$detail[$u_key + 1]['birth'] = $u_value['birth'];
//					$detail[$u_key + 1]['area_code'] = $u_value['area_name']; //$u_value['area_code'].' '.
//					$detail[$u_key + 1]['grade'] = $u_value['grade'];
//					$detail[$u_key + 1]['class_name'] = $u_value['class_name'];
					break;
				case 'c':
					$quota_table[$u_key + 1]['area_code'] = $u_value['area_name'];
					$quota_table[$u_key + 1]['school'] = $u_value['school_name'];
					$quota_table[$u_key + 1]['school_type'] = $u_value['school_type'];
					break;
				case 'd':
					$quota_table[$u_key + 1]['area_code'] = $u_value['area_name'];
					break;
			}
			//平均分
			$where['c.id'] = $u_value['id'];
			$temp = $score->table("quota b,
       							   score_user_quota a,
       							   groups_user c")
						  ->field("b.id,
						  		   round(ifnull(avg(a.score),0), 1) score ")
						  ->where($where)
						  ->group("b.id")
						  ->select();
//			dump($temp);
//			dump($score->_sql());
//			exit;
//			foreach($temp as $key => $value) {
//				$quota_table[$u_value['id']][$value['id']] = empty($value['score']) ? 0 : $value['score'];
//			}
//			$temp_id = i_array_column($temp, 'id');
			if(!empty($temp)) {
				foreach($quota_list as $key => $value) {
					$quota_score = 0;
					foreach($temp as $t_key => $t_value) {
						if($value['id'] == $t_value['id']) {
							$quota_score = $t_value['score'];
							unset($temp[$t_key]);
							break;
						}
					}
					$quota_table[$u_key + 1][$value['id']] = $quota_score;
				}
			} else {
				unset($quota_table[$u_value['id']]);
			}
		}
		$area_info = $score->field("replace(get_area_name('".$area_code."'), ' ', '_') area_name,
									get_school_name('".$school."') school_name")
						   ->find();
		$file_name = $area_info['area_name'].(!empty($school) ? $area_info['school_name'] : '').'_问卷详细数据（三级指标得分）';      	//excel文件名
		
		//将用户信息组装成表格导出到excel中
		$str = '';	//初始化
		//输出表头
		$str .= iconv("UTF-8","GBK",'<table border="1">');
		//循环输出行		
		foreach($quota_table as $v1) {
			$str .= iconv("UTF-8","GBK",'<tr>');
			//循环输出列
			foreach($v1 as $v2) {
				$temp_str = '<td style="vnd.ms-excel.numberformat:@">'.$v2.'</td>';
				$str .= iconv("UTF-8","GBK",$temp_str);
			}
			$str .= iconv("UTF-8","GBK",'</tr>');
		}
		$str .=	iconv("UTF-8","GBK",'</table>');
		 
		//输出
		ob_end_clean();//清除缓冲区,避免乱码
		header('Content-Length: '.strlen($str));
		header("content-Type:text/html; charset=gbk");
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:filename={$file_name}.xls");
		echo $str;
	}
	
	
	/**
	 * 问卷详细数据（指标项得分）
	 */
	public function download_second_quota() {
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		set_time_limit(0);
		ini_set("memory_limit", "-1");
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		$model = new FormModel();
		// 通过省市县校检索
		$groups_user = new GroupsUserModel();
		if(empty($user_type)) {
			$this->error("没有找到用户类型");
		}
		if($user_type == 'c' || $user_type == 'd')
		if(empty($area_code)) {
			$this->error("用户类型 必须为 学生 或 教师");
		}
		switch ($user_type) {
			case 'a':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'b':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'c':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
				break;
			case 'd':
				$where['area_code'] = $area_code;
				break;
			default:
				$this->error("错误的用户类型");
		}
		$where['type'] = $user_type;
		$where['is_useful'] = '1';
		$user_data = $groups_user->field("id,
										  type,
										  get_field_dict_name('groups_user','type',type) type_name,
										  real_name,
 							   			  get_field_dict_name('user', 'gender', gender) gender,
										  birth,
										  year(now()) - left(birth,4) age,
										  area_code,
										  get_area_name(area_code) area_name,
										  school,
										  get_school_name(school) school_name,
										  get_field_dict_name('groups_user','school_type',school_type) school_type,
										  get_field_dict_name('groups_user','phase',phase) phase,
										  get_field_dict_name('groups_user','grade',grade) grade,
										  get_class_name(class) class_name,
										  get_field_dict_name('groups_user','subject',subject) subject,
										  get_field_dict_name('groups_user','teach_year',teach_year) teach_year")
								 ->where($where)
								 ->order('area_code, school')
//								 ->limit(10)
								 ->select();
		//查询所有指标项
		$quota = new QuotaModel();
		$quota_list = $quota->field("distinct case when right(left(code, length(code)-1), 1) < '9' then left(code, length(code)-3) else left(code, length(code)-1) end code")
							->group("left(code, length(code)-1)")
							->order("substring(code, 1, 1), case when right(left(code, length(code)-1), 1) < '9' then right(left(code, length(code)-3), 1) else right(left(code, length(code)-1), 1) end, left(code, length(code)-1)")
							->select();
//		dump($quota_list);
//		exit;
		$quota_table = array();
		$head = array();   //表头数组
//		$detail[0]['name'] = "用户名";
		$quota_table[0]['province'] = "省";
		$quota_table[0]['city'] = "市";
		$quota_table[0]['area'] = "县";
		$quota_table[0]['real_name'] = "姓名";
		$quota_table[0]['gender'] = "性别";
		switch($user_type) {
			case 'a':
//				$detail[0]['birth'] = "出生年月";
//				$detail[0]['area_code'] = "所属县域";
				$quota_table[0]['phase'] = "学段";
				$quota_table[0]['school'] = "学校";
				$quota_table[0]['school_type'] = "学校类型";
				$quota_table[0]['grade'] = "年级";
				$quota_table[0]['class_name'] = "班级";
				$quota_table[0]['age'] = "年龄";
				break;
			case 'b':
				$quota_table[0]['phase'] = "学段";
				$quota_table[0]['school'] = "学校";
				$quota_table[0]['school_type'] = "学校类型";
				$quota_table[0]['age'] = "年龄";
				$quota_table[0]['teach_year'] = "教龄";
				$quota_table[0]['subject'] = "学科";
//				$detail[0]['birth'] = "出生年月";
//				$detail[0]['area_code'] = "所属县域";
//				$detail[0]['school'] = "所在学校";
//				$detail[0]['teach_year'] = "教龄";
//				$detail[0]['phase'] = "学段";
//				$detail[0]['subject'] = "教授学科";
//				$detail[0]['grade'] = "教授年级";
//				$detail[0]['class_name'] = "教授班级";
				break;
			case 'c':
				$quota_table[0]['area_code'] = "所属县域";
				$quota_table[0]['school'] = "所在学校";
				$quota_table[0]['school_type'] = "学校类型";
				break;
			case 'd':
				$quota_table[0]['area_code'] = "所属县域";
				break;
		}
//		dump($detail);
//		exit;
//		dump($question_array);
//		exit;
		foreach($quota_list as $key => $value) {
			$quota_table[0][$value['code']] = $value['code'];
		}
//		dump($quota_table);
//		exit;
//		dump($user);
		//组建指标表格
		$score = new ScoreModel();
		$where['_string'] = "a.quotaid = b.id and a.groups_userid = c.id";
		foreach($user_data as $u_key => $u_value) {
			$area_array = explode(' ', $u_value['area_name']);
			$quota_table[$u_key + 1]['province'] = $area_array[0];
			$quota_table[$u_key + 1]['city'] = $area_array[1];
			$quota_table[$u_key + 1]['area'] = $area_array[2];
			$quota_table[$u_key + 1]['real_name'] = $u_value['real_name'];
			$quota_table[$u_key + 1]['gender'] = $u_value['gender'];
			switch($user_type) {
				case 'a':
					$quota_table[$u_key + 1]['phase'] = $u_value['phase'];
					$quota_table[$u_key + 1]['school'] = $u_value['school_name'];
					$quota_table[$u_key + 1]['school_type'] = $u_value['school_type'];
					$quota_table[$u_key + 1]['grade'] = $u_value['grade'];
					$quota_table[$u_key + 1]['class_name'] = $u_value['class_name'];
					$quota_table[$u_key + 1]['age'] = $u_value['age'];
					break;
				case 'b':
					$quota_table[$u_key + 1]['phase'] = $u_value['phase'];
					$quota_table[$u_key + 1]['school'] = $u_value['school_name'];
					$quota_table[$u_key + 1]['school_type'] = $u_value['school_type'];
					$quota_table[$u_key + 1]['age'] = $u_value['age'];
					$quota_table[$u_key + 1]['teach_year'] = $u_value['teach_year'];
					$quota_table[$u_key + 1]['subject'] = $u_value['subject'];
//					$detail[$u_key + 1]['birth'] = $u_value['birth'];
//					$detail[$u_key + 1]['area_code'] = $u_value['area_name']; //$u_value['area_code'].' '.
//					$detail[$u_key + 1]['grade'] = $u_value['grade'];
//					$detail[$u_key + 1]['class_name'] = $u_value['class_name'];
					break;
				case 'c':
					$quota_table[$u_key + 1]['area_code'] = $u_value['area_name'];
					$quota_table[$u_key + 1]['school'] = $u_value['school_name'];
					$quota_table[$u_key + 1]['school_type'] = $u_value['school_type'];
					break;
				case 'd':
					$quota_table[$u_key + 1]['area_code'] = $u_value['area_name'];
					break;
			}
//			dump($detail);
//			exit;
			//个体求和
			$where['c.id'] = $u_value['id'];
			foreach($quota_list as $key => $value) {
				$where['b.code'] = array('like', '%'.$value['code'].'%');
				$temp = $score->table("quota b,
	       							   score_user_quota a,
	       							   groups_user c")
							  ->field("round(ifnull(sum(a.score),0), 2) score")
							  ->where($where)
							  ->group("b.code")
							  ->select();
				$temp_score = 0;
				foreach($temp as $t_key => $t_value) {
					$temp_score += $t_value['score'];
				}
				$quota_table[$u_key + 1][$value['code']] = $temp_score;
			}
		}
//		exit;
		//没有的值设置为 0
		$area_info = $score->field("replace(get_area_name('".$area_code."'), ' ', '_') area_name,
									get_school_name('".$school."') school_name")
						   ->find();
		$file_name = $area_info['area_name'].(!empty($school) ? $area_info['school_name'] : '').'_问卷详细数据（二级指标得分）';      	//excel文件名
		
		//将用户信息组装成表格导出到excel中
		$str = '';	//初始化
		//输出表头
		$str .= iconv("UTF-8","GBK",'<table border="1">');
		//循环输出行		
		foreach($quota_table as $v1) {
			$str .= iconv("UTF-8","GBK",'<tr>');
			//循环输出列
			foreach($v1 as $v2) {
				$temp_str = '<td style="vnd.ms-excel.numberformat:@">'.$v2.'</td>';
				$str .= iconv("UTF-8","GBK",$temp_str);
			}
			$str .= iconv("UTF-8","GBK",'</tr>');
		}
		$str .=	iconv("UTF-8","GBK",'</table>');
		 
		//输出
		ob_end_clean();//清除缓冲区,避免乱码
		header('Content-Length: '.strlen($str));
		header("content-Type:text/html; charset=gbk");
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:filename={$file_name}.xls");
		echo $str;
	}
	
	/**
	 * 问卷详细数据（个体阶段得分）
	 */
	public function download_first_quota() {
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		set_time_limit(0);
		ini_set("memory_limit", "-1");
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		$model = new FormModel();
		// 通过省市县校检索
		$groups_user = new GroupsUserModel();
		if(empty($user_type)) {
			$this->error("没有找到用户类型");
		}
		if($user_type == 'c' || $user_type == 'd')
		if(empty($area_code)) {
			$this->error("用户类型 必须为 学生 或 教师");
		}
		switch ($user_type) {
			case 'a':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'b':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
//				$where['used_time'] = array('gt', '00:05:00');
				break;
			case 'c':
				if(!empty($school)) {
					$where['school'] = $school;
				}
				if(!empty($area_code)) {
					$where['area_code'] = $area_code;
				}
				break;
			case 'd':
				$where['area_code'] = $area_code;
				break;
			default:
				$this->error("错误的用户类型");
		}
		$where['type'] = $user_type;
		$where['is_useful'] = '1';
		$user_data = $groups_user->field("id,
										  type,
										  get_field_dict_name('groups_user','type',type) type_name,
										  real_name,
 							   			  get_field_dict_name('user', 'gender', gender) gender,
										  birth,
										  year(now()) - left(birth,4) age,
										  area_code,
										  get_area_name(area_code) area_name,
										  school,
										  get_school_name(school) school_name,
										  get_field_dict_name('groups_user','school_type',school_type) school_type,
										  get_field_dict_name('groups_user','phase',phase) phase,
										  get_field_dict_name('groups_user','grade',grade) grade,
										  get_class_name(class) class_name,
										  get_field_dict_name('groups_user','subject',subject) subject,
										  get_field_dict_name('groups_user','teach_year',teach_year) teach_year")
								 ->where($where)
								 ->order('area_code, school')
//								 ->limit(10)
								 ->select();
		//查询所有指标项
		$quota = new QuotaModel();
		$quota_list = $quota->field("distinct case when right(left(code, length(code)-1), 1) < '9' then left(code, length(code)-5) else left(code, length(code)-3) end code")
							->group("left(code, length(code)-1)")
							->order("substring(code, 1, 1), case when right(left(code, length(code)-1), 1) < '9' then right(left(code, length(code)-5), 1) else right(left(code, length(code)-3), 1) end, left(code, length(code)-1)")
							->select();
//		dump($quota_list);
//		exit;
		$quota_table = array();
		$head = array();   //表头数组
//		$detail[0]['name'] = "用户名";
		$quota_table[0]['province'] = "省";
		$quota_table[0]['city'] = "市";
		$quota_table[0]['area'] = "县";
		$quota_table[0]['real_name'] = "姓名";
		$quota_table[0]['gender'] = "性别";
		switch($user_type) {
			case 'a':
//				$detail[0]['birth'] = "出生年月";
//				$detail[0]['area_code'] = "所属县域";
				$quota_table[0]['phase'] = "学段";
				$quota_table[0]['school'] = "学校";
				$quota_table[0]['school_type'] = "学校类型";
				$quota_table[0]['grade'] = "年级";
				$quota_table[0]['class_name'] = "班级";
				$quota_table[0]['age'] = "年龄";
				break;
			case 'b':
				$quota_table[0]['phase'] = "学段";
				$quota_table[0]['school'] = "学校";
				$quota_table[0]['school_type'] = "学校类型";
				$quota_table[0]['age'] = "年龄";
				$quota_table[0]['teach_year'] = "教龄";
				$quota_table[0]['subject'] = "学科";
//				$detail[0]['birth'] = "出生年月";
//				$detail[0]['area_code'] = "所属县域";
//				$detail[0]['school'] = "所在学校";
//				$detail[0]['teach_year'] = "教龄";
//				$detail[0]['phase'] = "学段";
//				$detail[0]['subject'] = "教授学科";
//				$detail[0]['grade'] = "教授年级";
//				$detail[0]['class_name'] = "教授班级";
				break;
			case 'c':
				$quota_table[0]['area_code'] = "所属县域";
				$quota_table[0]['school'] = "所在学校";
				$quota_table[0]['school_type'] = "学校类型";
				break;
			case 'd':
				$quota_table[0]['area_code'] = "所属县域";
				break;
		}
//		dump($detail);
//		exit;
//		dump($question_array);
//		exit;
		foreach($quota_list as $key => $value) {
			$quota_table[0][$value['code']] = $value['code'];
		}
//		dump($quota_table);
//		exit;
//		dump($user);
		//组建指标表格
		$score = new ScoreModel();
		$where['_string'] = "a.quotaid = b.id and a.groups_userid = c.id";
		foreach($user_data as $u_key => $u_value) {
			$area_array = explode(' ', $u_value['area_name']);
			$quota_table[$u_key + 1]['province'] = $area_array[0];
			$quota_table[$u_key + 1]['city'] = $area_array[1];
			$quota_table[$u_key + 1]['area'] = $area_array[2];
			$quota_table[$u_key + 1]['real_name'] = $u_value['real_name'];
			$quota_table[$u_key + 1]['gender'] = $u_value['gender'];
			switch($user_type) {
				case 'a':
					$quota_table[$u_key + 1]['phase'] = $u_value['phase'];
					$quota_table[$u_key + 1]['school'] = $u_value['school_name'];
					$quota_table[$u_key + 1]['school_type'] = $u_value['school_type'];
					$quota_table[$u_key + 1]['grade'] = $u_value['grade'];
					$quota_table[$u_key + 1]['class_name'] = $u_value['class_name'];
					$quota_table[$u_key + 1]['age'] = $u_value['age'];
					break;
				case 'b':
					$quota_table[$u_key + 1]['phase'] = $u_value['phase'];
					$quota_table[$u_key + 1]['school'] = $u_value['school_name'];
					$quota_table[$u_key + 1]['school_type'] = $u_value['school_type'];
					$quota_table[$u_key + 1]['age'] = $u_value['age'];
					$quota_table[$u_key + 1]['teach_year'] = $u_value['teach_year'];
					$quota_table[$u_key + 1]['subject'] = $u_value['subject'];
//					$detail[$u_key + 1]['birth'] = $u_value['birth'];
//					$detail[$u_key + 1]['area_code'] = $u_value['area_name']; //$u_value['area_code'].' '.
//					$detail[$u_key + 1]['grade'] = $u_value['grade'];
//					$detail[$u_key + 1]['class_name'] = $u_value['class_name'];
					break;
				case 'c':
					$quota_table[$u_key + 1]['area_code'] = $u_value['area_name'];
					$quota_table[$u_key + 1]['school'] = $u_value['school_name'];
					$quota_table[$u_key + 1]['school_type'] = $u_value['school_type'];
					break;
				case 'd':
					$quota_table[$u_key + 1]['area_code'] = $u_value['area_name'];
					break;
			}
//			dump($detail);
//			exit;
			//个体求和
			$where['c.id'] = $u_value['id'];
			foreach($quota_list as $key => $value) {
				$where['b.code'] = array('like', '%'.$value['code'].'%');
				$temp = $score->table("quota b,
	       							   score_user_quota a,
	       							   groups_user c")
							  ->field("round(ifnull(sum(a.score),0), 2) score")
							  ->where($where)
							  ->group("b.code")
							  ->select();
				$temp_score = 0;
				foreach($temp as $t_key => $t_value) {
					$temp_score += $t_value['score'];
				}
				$quota_table[$u_key + 1][$value['code']] = $temp_score;
			}
		}
//		exit;
		//没有的值设置为 0
		$area_info = $score->field("replace(get_area_name('".$area_code."'), ' ', '_') area_name,
									get_school_name('".$school."') school_name")
						   ->find();
		$file_name = $area_info['area_name'].(!empty($school) ? $area_info['school_name'] : '').'_问卷详细数据（一级指标得分）';      	//excel文件名
		
		//将用户信息组装成表格导出到excel中
		$str = '';	//初始化
		//输出表头
		$str .= iconv("UTF-8","GBK",'<table border="1">');
		//循环输出行		
		foreach($quota_table as $v1) {
			$str .= iconv("UTF-8","GBK",'<tr>');
			//循环输出列
			foreach($v1 as $v2) {
				$temp_str = '<td style="vnd.ms-excel.numberformat:@">'.$v2.'</td>';
				$str .= iconv("UTF-8","GBK",$temp_str);
			}
			$str .= iconv("UTF-8","GBK",'</tr>');
		}
		$str .=	iconv("UTF-8","GBK",'</table>');
		 
		//输出
		ob_end_clean();//清除缓冲区,避免乱码
		header('Content-Length: '.strlen($str));
		header("content-Type:text/html; charset=gbk");
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:filename={$file_name}.xls");
		echo $str;
	}
	
	/**
	 * 问卷详细数据（频数统计）
	 */
	public function download_score_num() {
		$user_type = $this->_request('type');
		$area_code = $this->_request('area_code');
		$school = $this->_request('school');
		echo "问卷详细数据（频数统计）";
	}
	
	/**
	 * 问卷数据表生成题项编号
	 */
	public function generate_questionid() {
		$model = new ItemValueModel();
		$num = $model->execute("update item_value 
								   set questionid = get_questionid(itemid) 
							     where questionid = ''
							        or questionid is null");
		if(false === $num) {
			$this->error("题项编号生成失败");
		}
		$this->success("题项编号生成成功，共影响 ".$num." 条记录", __APP__."?g=Admin&m=Score");
	}
	
	/**
	 * 问卷数据表生成题项类型
	 */
	public function generate_question_type() {
		$model = new ItemValueModel();
		$num = $model->execute("update item_value 
								   set type = get_question_type(itemid) 
							     where type = ''
							        or type is null");
		if(false === $num) {
			$this->error("题项类型生成失败");
		}
		$this->success("题项类型生成成功，共影响 ".$num." 条记录", __APP__."?g=Admin&m=Score");
	}
	
	/**
	 * 生成题项得分
	 */
	public function generate_question_score() {
		/**
		 * 得分规则：
		 * 1.如果选项有一个错，则整道题都不得分
		 * 2.对于完全正确得分，则无论选择的数量还是正确答案上，都必须完全正确才能得分
		 * 3.累计得分，选择正确的选项加起来的分值
		 */
		set_time_limit(0);
		ini_set("memory_limit", "-1");
		//查询最后一次生成时间
		$last_time = get_system_option('最后一次生成时间');
		//查询需要生成的问卷数据
		$model = new ItemValueModel();
		$item_value = $model->table("item_value a,
									question_test_select b,
									groups_user c")
						   ->field('distinct a.operator groups_userid,
						   			a.type,
									a.questionid,
									a.itemid,
									get_item_value(a.questionid, a.operator) value,
									b.score,
									b.score_rule')
						   ->where("a.questionid = b.id
						 		and a.operator = c.id
						        and a.operator is not null
						        and a.value is not null
								and a.question_operator not in (select question_operator from score)")
//						   ->limit(100000)
						   ->select();//and c.end_time >= '".$last_time."'
//		dump($model->_sql());
//		dump($item_value);
//		exit;
		$select_item = new QuestionTestSelectItemModel();
		$score_model = new ScoreModel();
		$score_model->startTrans();
		$user_score['operator'] = $model->getUserId();
		$user_score['update_date'] = $model->getDate();
		$num = 0;
		foreach($item_value as $key => $value) {
			switch ($value['type']) {
				case 'select':
					$item_value_list = $model->where("questionid = '".$value['questionid']."'
												  and operator = '".$value['groups_userid']."'")
											 ->getField("value", true);
					$item_value_string = "'".implode("','", $item_value_list)."'";
					//如果是完全正确得分
					if($value['score_rule'] == 'b') {
						$item_list_count = $select_item->field("id,
															  is_correct,
															  intro,
															  score item_score")
													 ->where("selectid = '".$value['questionid']."'
													 	  and is_correct = 'b'")
													 ->count();
						$item_list = $select_item->field("id,
														  is_correct,
														  intro,
														  score item_score")
												 ->where("selectid = '".$value['questionid']."'
												 	  and is_correct = 'b'
												 	  and intro in (".$item_value_string.")")
												 ->select();
						//如果选项有一个错，则整道题都不得分
						if(count($item_list) != $item_list_count || count($item_value_list) != $item_list_count) {
							$score = 0;
						} elseif(empty($item_list)) {
							$score = 0;
						} else {
							$score = $value['score'];
						}
					} else {
						//累计得分
						$score = $select_item->where("selectid = '".$value['questionid']."'
												 	  and intro in (".$item_value_string.")")
												 ->sum('score');
						if(empty($score)) {
							$score = 0;
						} elseif($score > $value['score']) {   //累计得分不能超过选项得分
							$score = $value['score'];
						}
					}
					break;
				case 'blank':
				case 'affix':
					$score = $model->where("questionid = '".$value['questionid']."'
									    and operator = '".$value['groups_userid']."'")
								   ->getField('score');
					if(empty($score)) {
						$score = 0;
					}
					break;
			}
			$user_score['groups_userid'] = $value['groups_userid'];
			$user_score['question_type'] = 'select';
			$user_score['questionid'] = $value['questionid'];
			$user_score['question_operator'] = $value['groups_userid'].'_'.$value['questionid'];
			$user_score['value'] = $value['value'];
			$user_score['score'] = $score;
			$user_score['total_score'] = $value['score'];
			//如果存在，则删除后再添加。
			$exits_score = $score_model->where("groups_userid = '".$value['groups_userid']."' and questionid = '".$value['questionid']."'")->getField("score");
			
			if(!empty($exits_score)) {
				if($exits_score != $score) {
					$score_model->where("groups_userid = '".$value['groups_userid']."' and questionid = '".$value['questionid']."'")->delete();
				} else {
					continue;
				}
			}
			if(false == $score_model->add($user_score)) {
				$this->error("添加得分数据失败");
				$score_model->rollback();
			}
			$score_model->commit();
			$num++;
			// 没 100 条成绩提交一次
			if($num % 100 == 0) {
				$score_model->commit();
			}
		}
		$this->success("题项得分生成成功，共添加 ".$num." 条得分记录", __APP__."?g=Admin&m=Score");
	}
	
	/**
	 * 生成score表中的question_seq列
	 */
	public function generate_score_question_seq() {
		set_time_limit(0);
		ini_set("memory_limit", "-1");
		$model = new CommonModel();
		$count = $model->execute("update score set question_seq = get_item_value_seq(questionid, groups_userid) where question_seq is null");
		if($count === false) {
			$this->error("生成失败");
		} else {
			$this->success("生成成功，生成  ".$count." 条记录");
		}
	}
	
	/**
	 * 生成score表中的item_seq列
	 */
	public function generate_score_item_seq() {
		set_time_limit(0);
		ini_set("memory_limit", "-1");
		$model = new CommonModel();
		$count = $model->execute("update score set item_seq = concat(replace(question_seq, '<br/>', ','), ',') where item_seq is null");
		if($count === false) {
			$this->error("生成失败");
		} else {
			$this->success("生成成功，生成  ".$count." 条记录");
		}
	}
	
	/**
	 * 生成用户-指标项得分（score_user_quota）
	 */
	public function generate_score_user_quota() {
		set_time_limit(0);
		ini_set("memory_limit", "-1");
		$grous_user = new GroupsUserModel();
		// 防止无休止的生成，暂定延津县
		$user_data = $grous_user->where("area_code = '530623'")->field("id")->select();
		$quota = new QuotaModel();
		$quota_data = $quota->field("id")->select();
		$model = new ScoreUserQuotaModel();
		$score = new ScoreModel();
		$i = 0;
//		dump($user_data);
		foreach($user_data as $u_key => $u_value) {
			foreach($quota_data as $q_key => $q_value) {
				$exits = $model->where("groups_userid = '".$u_value['id']."' 
									and quotaid = '".$q_value['id']."'")
							   ->find();
				if(!empty($exits)) {
					continue;
				}
				$temp = $score->field("c.first_quota,
									  c.second_quota,
									  c.stage,
									  c.code,
									  c.value quota")
							 ->table("score a,
									  quota_item b,
									  quota c")
							 ->where("a.questionid = b.questionid
							 	  and b.quotaid = c.id
							 	  and a.groups_userid = '".$u_value['id']."'
							 	  and b.quotaid = '".$q_value['id']."'")
							  ->find();
				if(!empty($temp)) {
					$sum = $score->field("round(ifnull(sum(a.score),0),2) score,
										  c.first_quota,
										  c.second_quota,
										  c.stage,
										  c.code,
										  c.value quota")
								 ->table("score a,
										  quota_item b,
										  quota c")
								 ->where("a.questionid = b.questionid
								 	  and b.quotaid = c.id
								 	  and a.groups_userid = '".$u_value['id']."'
								 	  and b.quotaid = '".$q_value['id']."'")
								 ->find();
					$temp = $model->create($sum);
					$temp['groups_userid'] = $u_value['id'];
					$temp['quotaid'] = $q_value['id'];
					if(false === $model->add($temp)) {
						$this->error("生成失败");
					}
					$grous_user->where("id = '".$u_value['id']."'")->setField('score_user_quota', '1');
					unset($temp);
					unset($sum);
					$i++;
				} else {
					// 被受控制题目，虽然没有做题，但是依然作为人员群体，作为分母
//					$reedit = array("cms_def_webid_20160908210113_1k2jeh4xsp",
//									"cms_def_webid_20160908200809_efu9dc4q9wo",
//									"cms_def_webid_20160908200853_ajt0t3qjm4w",
//									"cms_def_webid_20160908201030_9pdinjoo660");
//					if(in_array($q_value['id'], $reedit)) {
						$sum = $score->field("round(ifnull(sum(a.score),0),2) score,
											  c.first_quota,
											  c.second_quota,
											  c.stage,
											  c.code,
											  c.value quota")
									 ->table("score a,
											  quota_item b,
											  quota c")
									 ->where("a.questionid = b.questionid
									 	  and b.quotaid = c.id
									 	  and a.groups_userid = '".$u_value['id']."'
									 	  and b.quotaid = '".$q_value['id']."'")
									 ->find();
						$temp = $model->create($sum);
						$temp['groups_userid'] = $u_value['id'];
						$temp['quotaid'] = $q_value['id'];
						if(false === $model->add($temp)) {
							$this->error("生成失败");
						}
						$grous_user->where("id = '".$u_value['id']."'")->setField('score_user_quota', '1');
						unset($temp);
						unset($sum);
						$i++;
//					}
				}
			}
		}
		$this->success("生成成功");
	}
	public function scan_ztdf(){
		$id = $this->_request("id");
		//查询总分
		$model = new ScoreTestModel();
		$all_score_df = $model->getField("sum(hsdf)");
		$all_score = get_system_option("问卷总分");
		$score=  round($all_score_df/$all_score,2);
		$this->assign('score', $score); 
		$vals = "";
		if ($score=== 0.875) {
			$vals = '优';
		}
		else if ($score=== 0.625) {
			$vals = '中';
		}
		else if ($score=== 0.375) {
			$vals = '良';
		}
		else if ($score=== 0.125) {
			$vals = '差';
		}
		$this->assign('vals', $vals);
		$this->display();
	}
	public function scan_ztdf_old(){
		$id = $this->_request("id");
		//查询总分
		$model = new ScoreTestModel();
		$all_score_df = $model->getField("sum(hsdf)");
		$all_score = get_system_option("问卷总分");
		$score=  round($all_score_df/$all_score,2);
		$this->assign('score', $score);
		$vals = "";
		if ($score=== 0.875) {
			$vals = '优';
		}
		else if ($score=== 0.625) {
			$vals = '中';
		}
		else if ($score=== 0.375) {
			$vals = '良';
		}
		else if ($score=== 0.125) {
			$vals = '差';
		}
		$this->assign('vals', $vals);
		$this->display();
	}
	public function scan_yjzb(){
		//查询一级指标各项得分
		$model = new ScoreTestModel();
		$I1_all = $model->where("cptx like 'I%'")->getField("sum(hszf)");
		$I1_all = round($I1_all,2);
		$this->assign('I1_all', $I1_all);
		$I1_all_df = $model->where("cptx like 'I%'")->getField("sum(hsdf)");
		$I1_all_df= round($I1_all_df,2);
		$this->assign('I1_all_df', $I1_all_df);
		$I1_dfv = round($I1_all_df/$I1_all,2);
		$this->assign('I1_dfv', $I1_dfv);
		$IM_all = $model->where("cptx like 'M%'")->getField("sum(hszf)");
		$IM_all= round($IM_all,2);
		$IM_all_df = $model->where("cptx like 'M%'")->getField("sum(hsdf)");
		$IM_all_df= round($IM_all_df,2);
		$IM_dfv = round($IM_all_df/$IM_all,2);
		$this->assign('IM_dfv', $IM_dfv);
		$IK_all = $model->where("cptx like 'K%'")->getField("sum(hszf)");
		$IK_all= round($IK_all,2);
		$IK_all_df = $model->where("cptx like 'K%'")->getField("sum(hsdf)");
		$IK_all_df= round($IK_all_df,2);
		$IK_dfv = round($IK_all_df/$IK_all,2);
		$this->assign('IK_dfv', $IK_dfv);
		$IR_all = $model->where("cptx like 'R%'")->getField("sum(hszf)");
		$IR_all= round($IR_all,2);
		$IR_all_df = $model->where("cptx like 'R%'")->getField("sum(hsdf)");
		$IR_all_df= round($IR_all_df,2);
		$IR_dfv = round($IR_all_df/$IR_all,2);
		$this->assign('IR_dfv', $IR_dfv);
		$score_all = $I1_all.",".$IM_all.",".$IK_all.",".$IR_all;
		$score_all_df = $I1_all_df.",".$IM_all_df.",".$IK_all_df.",".$IR_all_df;
		$this->assign('score_all', $score_all);
		$this->assign('score_all_df', $score_all_df);
		$first_string = "{value: ".$I1_dfv.", name: '理想信念（".$I1_dfv."）'},{value: ".$IM_dfv.", name: '道德情操（".$IM_dfv."）'},{value: ".$IK_dfv.", name: '扎实学识（".$IK_dfv."）'},{value: ".$IR_dfv.", name: '仁爱之心（".$IR_dfv."）'}";
		$this->assign('first_string', $first_string);
		$this->display();
	}
	public function scan_yjzb_old(){
		//查询一级指标各项得分
		$model = new ScoreTestModel();
		$I1_all = $model->where("cptx like 'I%'")->getField("sum(hszf)");
		$I1_all = round($I1_all,2);
		$this->assign('I1_all', $I1_all);
		$I1_all_df = $model->where("cptx like 'I%'")->getField("sum(hsdf)");
		$I1_all_df= round($I1_all_df,2);
		$this->assign('I1_all_df', $I1_all_df);
		$I1_dfv = 100*round($I1_all_df/$I1_all,2);
		$this->assign('I1_dfv', $I1_dfv);
		$IM_all = $model->where("cptx like 'M%'")->getField("sum(hszf)");
		$IM_all= round($IM_all,2);
		$IM_all_df = $model->where("cptx like 'M%'")->getField("sum(hsdf)");
		$IM_all_df= round($IM_all_df,2);
		$IM_dfv = round($IM_all_df/$IM_all,2)*100;
		$this->assign('IM_dfv', $IM_dfv);
		$IK_all = $model->where("cptx like 'K%'")->getField("sum(hszf)");
		$IK_all= round($IK_all,2);
		$IK_all_df = $model->where("cptx like 'K%'")->getField("sum(hsdf)");
		$IK_all_df= round($IK_all_df,2);
		$IK_dfv = round($IK_all_df/$IK_all,2)*100;
		$this->assign('IK_dfv', $IK_dfv);
		$IR_all = $model->where("cptx like 'R%'")->getField("sum(hszf)");
		$IR_all= round($IR_all,2);
		$IR_all_df = $model->where("cptx like 'R%'")->getField("sum(hsdf)");
		$IR_all_df= round($IR_all_df,2);
		$IR_dfv = round($IR_all_df/$IR_all,2)*100;
		$this->assign('IR_dfv', $IR_dfv);
		$score_all = $I1_all.",".$IM_all.",".$IK_all.",".$IR_all;
		$score_all_df = $I1_all_df.",".$IM_all_df.",".$IK_all_df.",".$IR_all_df;
		$this->assign('score_all', $score_all);
		$this->assign('score_all_df', $score_all_df);
		$first_string = "{value: ".$I1_dfv.", name: '理想信念（".$I1_dfv."%）'},{value: ".$IM_dfv.", name: '道德情操（".$IM_dfv."%）'},{value: ".$IK_dfv.", name: '扎实学识（".$IK_dfv."%）'},{value: ".$IR_dfv.", name: '仁爱之心（".$IR_dfv."%）'}";
		$this->assign('first_string', $first_string);
		$first_string1 = "{value: ".$IR_dfv.", name: '理想信念'},{value: ".(100-$I1_dfv).", name: '剩余'}";
		$this->assign('first_string1', $first_string1);
		$first_string2 = "{value: ".$IR_dfv.", name: '道德情操'},{value: ".(100-$IM_dfv).", name: '剩余'}";
		$this->assign('first_string2', $first_string2);
		$first_string3 = "{value: ".$IR_dfv.", name: '扎实学识'},{value: ".(100-$IK_dfv).", name: '剩余'}";
		$this->assign('first_string3', $first_string3);
		$first_string4 = "{value: ".$IR_dfv.", name: '仁爱之心'},{value: ".(100-$IR_dfv).", name: '剩余'}";
		$this->assign('first_string4', $first_string4);
	}
	public function scan_ejzb(){
		//查询二级指标各项得分
		$code = $this->_request("code");
		$field_model = new FieldDictModel();
		$yjzb_name = $field_model->where("field_value='".$code."'")->getField("field_mean");
		$this->assign('yjzb_name', $yjzb_name);
		$model = new ScoreTestModel();
		$quota_model = new QuotaModel();
		//查询一级指标对应的二级指标
		$second_data = $quota_model->field("second_quota code,get_field_dict_name('quota', 'second_quota', second_quota) second_quota_name")->where("first_quota='".$code."'")->group("second_quota")->select();
		//echo $quota_model->getLastSql();
		$this->assign('second_data', $second_data);
		//dump($second_data);
		//exit;
		//获取对应二级指标得分及总分{ text: '理想信念', max: 150 }
		foreach($second_data as $key=>$value){
			$score_all[$key] = $model->where("cptx like '".$value['code']."%'")->getField("sum(hszf)");
			$score_all[$key] = round($score_all[$key],2);
			$score_all_df[$key] = $model->where("cptx like '".$value['code']."%'")->getField("sum(hsdf)");
			$score_all_df[$key] = round($score_all_df[$key],2);
			$ejzb_dfl[$key] = round(round($score_all_df[$key]/$score_all[$key],2),2);
			$second_quota_data1[$key] = " { text: '".$value['second_quota_name']."（".$ejzb_dfl[$key]."）"."' }";
			$second_quota_data2[$key] = " { text: '".$value['second_quota_name']."（".$ejzb_dfl[$key]."）"."', max: 40 }";
			$second_quota_data3[$key] = " { value: ".$ejzb_dfl[$key].", name: '".$value['second_quota_name']."（".$ejzb_dfl[$key]."）"."'}";
		}
		//数组转成字符串
		$second_quota_string1 = implode(",",$second_quota_data1);
		$this->assign('second_quota_string1', $second_quota_string1);
		$second_quota_string2 = implode(",",$second_quota_data2);
		$this->assign('second_quota_string2', $second_quota_string2);
		$second_quota_string3 = implode(",",$second_quota_data3);
		$this->assign('second_quota_string3', $second_quota_string3);
		$score_all_string = implode(",",$score_all);
		$this->assign('score_all_string', $score_all_string);
		$score_all_df_string = implode(",",$score_all_df);
		$this->assign('score_all_df_string', $score_all_df_string);
		//dump($score_all_df_string);
		//exit;
		$this->display();
	}
	public function scan_sjzb(){
		//查询二级指标各项得分
		$code = $this->_request("code");
		$this->assign('code', $code);
		$field_model = new FieldDictModel();
		$ejzb_name = $field_model->where("field_value='".$code."'")->getField("field_mean");
		$this->assign('ejzb_name', $ejzb_name);
		$model = new ScoreTestModel();
		$quota_model = new QuotaModel();
		$yjzb_code = $quota_model->where("second_quota='".$code."'")->getField("first_quota");
		$this->assign('yjzb_code', $yjzb_code);
		
		//查询一级指标对应的二级指标
		$third_data = $quota_model->field("third_quota code,get_field_dict_name('quota', 'third_quota', third_quota) name")->where("second_quota='".$code."'")->group("third_quota")->select();
		//echo $quota_model->getLastSql();
		$this->assign('third_data', $third_data);
		//dump($second_data);
		//exit;
		//获取对应二级指标得分及总分{ text: '理想信念', max: 150 }
		foreach($third_data as $key=>$value){
			$score_all[$key] = $model->where("cptx like '".$value['code']."%'")->getField("sum(hszf)");
			$score_all[$key] = round($score_all[$key],2);
			$score_all_df[$key] = $model->where("cptx like '".$value['code']."%'")->getField("sum(hsdf)");
			$score_all_df[$key] = round($score_all_df[$key],2);
			$ejzb_dfl[$key] = round(round($score_all_df[$key]/$score_all[$key],2),2);
			$second_quota_data1[$key] = " { text: '".$value['name']."（".$ejzb_dfl[$key]."）"."' }";
			$second_quota_data2[$key] = " { text: '".$value['name']."（".$ejzb_dfl[$key]."）"."', max: 40 }";
			$second_quota_data3[$key] = " { value: ".$ejzb_dfl[$key].", name: '".$value['name']."（".$ejzb_dfl[$key]."）"."'}";
		}
		//数组转成字符串 {value: 40, name: 'rose 1'}
		$second_quota_string1 = implode(",",$second_quota_data1);
		$this->assign('second_quota_string1', $second_quota_string1);
		$second_quota_string2 = implode(",",$second_quota_data2);
		$this->assign('second_quota_string2', $second_quota_string2);
		$second_quota_string3 = implode(",",$second_quota_data3);
		$this->assign('second_quota_string3', $second_quota_string3);
		//dump($second_quota_string3);
		//exit;
		$score_all_string = implode(",",$score_all);
		$this->assign('score_all_string', $score_all_string);
		$score_all_df_string = implode(",",$score_all_df);
		$this->assign('score_all_df_string', $score_all_df_string);
		//dump($score_all_df_string);
		//exit;
		$this->display();
	}
	public function scan_dffx(){
		//查询当前信息
		$id = $this->_request("id");
		$users_model = new Groups_userModel();
		$users_list = $users_model->field("id,
 							   groupsid,
 		                       name,
 		                       pwd,
 		                       real_name,
 		                       get_field_dict_name('user', 'gender', gender) gender,
 		                       get_field_dict_name('groups_user', 'professional', professional) professional,
 							   get_field_dict_name('teacher', 'type', profession) profession,
  							   get_colledge_name(college) college_name,
 		                       duty,
 		                       certificate_no,
 		                       phone,
 		                       email,
 		                       get_formate_time(begin_time) begin_time,
 		                       get_formate_time(end_time) end_time,
 		                       time_format(timediff(end_time, begin_time),'%H:%i:%s') used_time,
 		                       get_user_name(operator) operator,
    						   update_date")->where("id = '".$id."'")->find();
		$this->assign('users_list', $users_list);
		//查询总分
		$model = new ScoreTestModel();
		$all_score_df = $model->getField("sum(hsdf)");
		$all_score = get_system_option("问卷总分");
		$score=  round($all_score_df/$all_score,2);
		$this->assign('score', $score);
		$vals = "";
		if ($score=== 0.875) {
			$vals = '优';
		}
		else if ($score=== 0.625) {
			$vals = '中';
		}
		else if ($score=== 0.375) {
			$vals = '良';
		}
		else if ($score=== 0.125) {
			$vals = '差';
		}
		$this->assign('vals', $vals);
		$this->scan_yjzb_old();
		//查询所有的二级指标
		$model = new ScoreTestModel();
		$quota_model = new QuotaModel();
		$second_data = $quota_model->field("second_quota code,get_field_dict_name('quota', 'second_quota', second_quota) second_quota_name")->group("second_quota")->order("second_quota")->select();
		//echo $quota_model->getLastSql();
		$this->assign('second_data', $second_data);
		
		//获取对应二级指标得分及总分{ text: '理想信念', max: 150 }
		foreach($second_data as $key=>$value){
			$second_data_name[$key] = $value['second_quota_name'];
			$score_all[$key] = $model->where("cptx like '".$value['code']."%'")->getField("sum(hszf)");
			$score_all[$key] = round($score_all[$key],2);
			$score_all_df[$key] = $model->where("cptx like '".$value['code']."%'")->getField("sum(hsdf)");
			$score_all_df[$key] = round($score_all_df[$key],2);
			$ejzb_dfl[$key] = round(round($score_all_df[$key]/$score_all[$key],2),2)*100;
			$second_quota_data1[$key] = " { text: '".$value['second_quota_name']."（".$ejzb_dfl[$key]."）"."' }";
			$second_quota_data2[$key] = " { text: '".$value['second_quota_name']."（".$ejzb_dfl[$key]."）"."', max: 40 }";
			$second_quota_data3[$key] = " { value: ".$ejzb_dfl[$key].", name: '".$value['second_quota_name']."（".$ejzb_dfl[$key]."）"."'}";
			$third_quota_data[$key] = "{product: '".$value['second_quota_name']."',";
			$second_dfl_jxt[$key] = "{name: '二级指标：".$value['second_quota_name']."（".$ejzb_dfl[$key]."%）"."', value: ".$ejzb_dfl[$key].",children: [";
			//查询三级指标
			$third_data = $quota_model->field("third_quota code,get_field_dict_name('quota', 'third_quota', third_quota) name")->where("second_quota='".$value['code']."'")->group("third_quota")->select();
			foreach($third_data as $k=>$val){
				$score_all_sj[$k] = $model->where("cptx like '".$val['code']."%'")->getField("sum(hszf)");
				$score_all_sj[$k] = round($score_all_sj[$k],2);
				$score_all_df_sj[$k] = $model->where("cptx like '".$val['code']."%'")->getField("sum(hsdf)");
				$score_all_df[$k] = round($score_all_df_sj[$k],2);
				$sjzb_dfl_sj[$k] = round(round($score_all_df_sj[$k]/$score_all_sj[$k],2),2)*100;
				$sjzb_dfl_sjs[$k] = $val['name']."': ".$sjzb_dfl_sj[$k];
				$third_dfl_jxt[$k] = "{name: '".$val['name']."（".$sjzb_dfl_sj[$k]."%）"."',value: ".$sjzb_dfl_sj[$k]."}";
			}
			$sjzb_dfl_string = implode(",'",$sjzb_dfl_sjs);
			$sjzb_ejzb_string[$key] =$third_quota_data[$key]."'".$sjzb_dfl_string."}";
			$third_dfl_jxt_string = implode(",",$third_dfl_jxt);
			$third_dfl_jxt_data[$key] = $second_dfl_jxt[$key].$third_dfl_jxt_string."]}";
		}
		
		$second_quota_string = implode("','",$second_data_name);
		//dump("'".$second_quota_string."'");
		//exit;
		$this->assign('second_quota_string', "'".$second_quota_string."'");
		$score_all_df_string = implode(",",$ejzb_dfl);
		//dump($score_all_df_string);
		//exit;
		$this->assign('score_all_df_string',$score_all_df_string);
		$sjzb_last_string = implode(",",$sjzb_ejzb_string);
		//dump($sjzb_last_string);
		//exit;
		$this->assign('sjzb_last_string',$sjzb_last_string);
		$third_data = $quota_model->field("third_quota code,get_field_dict_name('quota', 'third_quota', third_quota) name")->group("third_quota")->select();
		foreach($third_data as $ke=>$v){
			$third_data_arr[$ke] = $v['name'];
		}
		$third_data_string = implode("','",$third_data_arr);
		$third_data_string = "'product','".$third_data_string."'";
		$this->assign('third_data_string',$third_data_string);
		$third_dfl_jxt_last_string= implode(",",$third_dfl_jxt_data);
		$this->assign('third_dfl_jxt_last_string',$third_dfl_jxt_last_string);
		$this->display();
	}
}
?>